﻿using System;
using System.Collections.Generic;
using System.Data;
using Besture.Bll;
using Besture.Common;
using Besture.Common.Helper;
using Besture.Common.Solution;
using Besture.UI.Admin.Master;
using Besture.UI.ClassHelper;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.Data;

namespace Besture.UI.Admin.CommonModel
{
    public partial class ModelList : BasePage
    {
        #region 页面加载

        /// <summary>
        /// 初始化页面
        /// </summary>
        protected override void InitialLoad()
        {
            ((ConfigAdmin)this.Master).Title = "内容模型管理";
            BindData();
            AspxGridViewHelper.InitAspxGridView(grid);
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {
            BindData();
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        public void BindData()
        {
            ContentModelDac ContentModelDac = new ContentModelDac();
            grid.DataSource = ContentModelDac.GetDataTable();
            grid.DataBind();
        }

        /// <summary>
        /// AspxGridView回发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grid_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
            if (e.Parameters == "Refresh")
                grid.DataBind();
        }

        #endregion

        #region 删除数据

        /// <summary>
        /// 删除单条数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grid_RowDeleting(object sender, ASPxDataDeletingEventArgs e)
        {
            e.Cancel = true;
            int ID = Convert.ToInt32(e.Keys[0]);
            ContentModelDac ContentModelDac = new ContentModelDac();
            ContentModelDac.Delete(ID);
            BindData();
        }

        #endregion

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender,EventArgs e)
        {
            Response.Redirect("ModelSet.aspx");
        }

        /// <summary>
        /// 生成选中的内容模型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbCreate_Click(object sender, EventArgs e)
        {
            string ids = GetCheckID().TrimEnd(',');
            if (string.IsNullOrEmpty(ids))
            {
                return;
            }
            //获取选中内容模板
            string sql =
                string.Format(
                    @"SELECT ID, Name, TableName, ProjectName, ProjectRemark, ContentTemplate
                    FROM TBContentModel WHERE ID IN ({0})",
                    ids);
            DataTable tableContentModel = QueryDac.GetTable(sql);


            for (int i = 0; i < tableContentModel.Rows.Count; i++)
            {
                //替换Proj文件的字符串
                DataTable tableContentModelField = QueryDac.GetTable("SELECT * FROM TBContentModelField WHERE ModelID = " + tableContentModel.Rows[i]["ID"]);

                AutoMaticHelper.CreateManagePage(tableContentModel.Rows[i]["TableName"].ToString(), tableContentModelField);
            }

            SolutionManager.UpdateCsProj();

            PageAlert("生成成功，建议点击管理页面进行测试！");
        }




        
        /// <summary>
        /// 获得多选框ID
        /// </summary>
        /// <returns></returns>
        private string GetCheckID()
        {
            string rValue = "";
            try
            {
                List<object> values = grid.GetSelectedFieldValues(new[] { grid.KeyFieldName });
                List<string> str = new List<string>(values.Count); //初始化
                for (int i = 0; i < values.Count; i++)
                    str.Add(values[i].ToString());
                foreach (string tempStr in str)
                    rValue += tempStr + ",";

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return rValue;
        }

        

       
    }
}